package commonClass;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

public class DataLink {
	// private Context context;

	private HttpClient httpClient = new DefaultHttpClient();
	private HttpPost httpPost = new HttpPost();
	private String baseUrl = "";
	private ProgressDialog progressDialog = null;
	// private String Urlofzhengzhuangfz =
	// "http://59.68.29.72:8081/AnimalDoctor/querySymptomsCategorieByAnimalIDAction?appKey=b6d0027729f5c6a3&animalID=01371054603";
	private static String Url = "http://172.16.0.206/AnimalDoctor/";
	private static String appKey = "appKey=b6d0027729f5c6a3";

	/*
	 * public DataLink(Context context){ this.context = context; }
	 */

	public void post(final String methodName, final String paramters,
			final ICompeletListener compeletListener, final Context context) {
		final Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				compeletListener.doCompelet((String) msg.obj);
			}
		};
		new Thread() {

			public void run() {

				String result = "";

				System.out.print("####################" + Url + methodName
						+ "?" + appKey + "&" + paramters);

				/*
				 * try { baseUrl =URLEncoder.encode(paramters, "UTF-8");
				 * 
				 * } catch (UnsupportedEncodingException e1) { // TODO
				 * Auto-generated catch block e1.printStackTrace(); }
				 * 
				 * System.out.println("####################**********:=======" +
				 * baseUrl );
				 */
				// Url+methodName+"?" + appKey+"&" + paramters

				httpPost = new HttpPost(Url + methodName + "?" + appKey + "&"
						+ paramters);

				httpPost.setEntity(null);

				try {
					HttpResponse httpResponse = httpClient.execute(httpPost);
					if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
						result = EntityUtils.toString(httpResponse.getEntity());
						Message message = handler.obtainMessage(1, result);
						handler.sendMessage(message);
					} else {
						progressDialog = ProgressDialog.show(context, null,
								"正在登陆");
					}
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}.start();

	}

	public interface ICompeletListener {
		public void doCompelet(String result);
	}

}
